package gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.SpinnerListModel;

import logic.Facade;
import logic.ToGuiFacade;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class pnlAddRating extends javax.swing.JPanel {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JTextArea addRatingTextArea;
	private JLabel picLabel;
//	private JComboBox movieComboBox;
	private JTextArea movieTextArea;
	private JButton ratingButton;
	private JButton removeRatingBut;
	private JButton btnBack;
	private JScrollPane jScrollPane3;
	private JTextPane descriptionTextPane;
	private JSpinner ratingSpinner;
	private JTextArea descriptionTextArea;
	private JTextArea ratingTextArea;
	private JTextArea movieNameTextArea;
	private String _user;
	private String[] _seen;
	private static String _movie;

	public pnlAddRating(String user) {
		super();
		_user=user;
		//_seen=false;
		//_ratings=logic.Facade.getInstance().getUserRatings(_user);
		initGUI(user, "");
	}
	public pnlAddRating(String user, String movie) {
		super();
		_user=user;
		_movie=movie;
		//_seen=false;
		//_ratings=logic.Facade.getInstance().getUserRatings(_user);
		initGUI(user, movie);
	}

	private void initGUI(final String user, String movie) {
		_seen = logic.Facade.getInstance().haveSeenRating(_user, _movie);
		try {
			this.setPreferredSize(new java.awt.Dimension(620, 363));
			this.setLayout(null);
			this.setBackground(new java.awt.Color(255,128,0));
			{
				addRatingTextArea = new JTextArea();
				this.add(addRatingTextArea);
				addRatingTextArea.setText("Rate a movie");
				addRatingTextArea.setBounds(186, 35, 143, 32);
				addRatingTextArea.setFont(new java.awt.Font("Arial",1,22));
				addRatingTextArea.setOpaque(false);
			}
			{
				picLabel = new JLabel();
				this.add(picLabel);
				picLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("documentation/miniSecret4PlusDog.JPG")));
				picLabel.setBounds(460, 23, 132, 237);
			}
			{
				movieNameTextArea = new JTextArea();
				this.add(movieNameTextArea);
				movieNameTextArea.setText(movie);
				movieNameTextArea.setBounds(130, 98, 282, 21);
				movieNameTextArea.setFont(new java.awt.Font("Arial",1,22));
				movieNameTextArea.setOpaque(false);
				movieNameTextArea.setForeground(Color.BLUE);
			}
			{
				movieTextArea = new JTextArea();
				this.add(movieTextArea);
				movieTextArea.setText("movie:");
				movieTextArea.setBounds(46, 99, 85, 18);
				movieTextArea.setOpaque(false);
				movieTextArea.setFont(new java.awt.Font("Arial",1,14));
			}
			{
				ratingTextArea = new JTextArea();
				this.add(ratingTextArea);
				ratingTextArea.setText("rating:");
				ratingTextArea.setBounds(46, 140, 90, 18);
				ratingTextArea.setEditable(false);
				ratingTextArea.setOpaque(false);
				ratingTextArea.setFont(new java.awt.Font("Arial",1,14));
			}
			{
				descriptionTextArea = new JTextArea();
				this.add(getDescriptionTextArea());
				descriptionTextArea.setText("description:");
				descriptionTextArea.setBounds(46, 193, 90, 18);
				descriptionTextArea.setEditable(false);
				descriptionTextArea.setOpaque(false);
				descriptionTextArea.setFont(new java.awt.Font("Arial",1,14));
			}
			{
				SpinnerListModel ratingSpinnerModel =
					new SpinnerListModel(
							new String[] { "1", "2" , "3" , "4" , "5"  });
				ratingSpinner = new JSpinner();
				this.add(getRatingSpinner());
				ratingSpinner.setModel(ratingSpinnerModel);
				if(_seen[0]!=null)
					ratingSpinner.setValue(_seen[0]);
				ratingSpinner.setBounds(176, 140, 35, 21);
			}
			{
				jScrollPane3 = new JScrollPane();
				this.add(jScrollPane3);
				jScrollPane3.setBounds(176, 193, 169, 98);
				{
					descriptionTextPane = new JTextPane();
					jScrollPane3.setViewportView(descriptionTextPane);
					descriptionTextPane.setBounds(176, 193, 169, 98);
					if(_seen[0]!=null)
						descriptionTextPane.setText(_seen[1]);
				}
			}
			{
				ratingButton = new JButton();
				this.add(ratingButton);
				if(_seen[0]==null){
					ratingButton.setText("Add your rating");
					ratingButton.setBounds(228, 320, 172, 21);
					ratingButton.setVisible(true);
					ratingButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							Facade.getInstance().addRating(user, _movie , (String)ratingSpinner.getValue(), descriptionTextPane.getText());
						}
					});
				}
				else{
					ratingButton.setText("Change your rating");
					ratingButton.setBounds(228, 320, 172, 21);
					ratingButton.setVisible(true);
					ratingButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e){
							Facade.getInstance().changeRating(user, _movie , (String)ratingSpinner.getValue(), descriptionTextPane.getText());
						}
					});

				}
			}
			{
				btnBack = new JButton();
				this.add(btnBack);
				btnBack.setText("back");
				btnBack.setBounds(39, 320, 172, 21);
				btnBack.setVisible(true);
				btnBack.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnBackActionPerformed(evt);
					}
				});
			}
			{
				removeRatingBut = new JButton();
				this.add(removeRatingBut);
				removeRatingBut.setText("Remove current rating");
				removeRatingBut.setBounds(416, 320, 169, 21);
				removeRatingBut.setVisible(true);
				if(_seen[0]==null)
					removeRatingBut.setVisible(false);
				removeRatingBut.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						removeRatingButActionPerformed(evt);
					}
				});
				//removeRatingBut.setVisible(false);
				//
			}
			ToGuiFacade.getInstance().setAddRatingScreen(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public JTextArea getDescriptionTextArea() {
		return descriptionTextArea;
	}

	public JSpinner getRatingSpinner() {
		return ratingSpinner;
	}



	public JTextPane getDescriptionTextPane() {
		return descriptionTextPane;
	}

	private void btnBackActionPerformed(ActionEvent evt) {

			ToGuiFacade.getInstance().back();
			//ToGuiFacade.getInstance().showUserScreen(_user, true);



	}

	public void setRemoveRatingBut(boolean hasSeenMovie) {
		// TODO Auto-generated method stub
		removeRatingBut.setVisible(hasSeenMovie);
	}

	private void removeRatingButActionPerformed(ActionEvent evt) {
		//TODO add your code for removeRatingBut.actionPerformed
		System.out.println(_movie);
		//logic.Facade.getInstance().removeRating(_movie, _user);
		JOptionPane.showMessageDialog(null, "Diabled for now to protect databse");
		ToGuiFacade.getInstance().showUserScreenAfterRating(_user,true);
	}


}
